package com.ztgf.order.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.common.utils.DateUtils;
import com.ztgf.order.common.config.YmlConfig;
import com.ztgf.order.mapper.OrderExpirationDateMapper;
import com.ztgf.order.models.bo.OrderExpirationDateSaveBO;
import com.ztgf.order.models.dto.OrderExpirationDateDTO;
import com.ztgf.order.models.service.IOrderExpirationDateService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 订单有效期控制器
 *
 * @author chenyu.wang
 * @Date 2020-06-03 15:56:18
 */
@DubboService
public class OrderExpirationDateServiceImpl extends ServiceImpl<OrderExpirationDateMapper, OrderExpirationDateDTO> implements IOrderExpirationDateService {

    @Resource
    private YmlConfig ymlConfig;


    /**
     * 获取所有到期的订单 根据到期时间
     *
     * @return
     */
    @Override
    public List<OrderExpirationDateDTO> getAllExpireOrderInfo(Long lastNo) {
        //return baseMapper.getAllExpireOrderInfo(lastNo);
        // 当前时间
        String currentTime = DateUtils.dateToStr(DateUtils.getCurrentDateTime(), DateUtils.DATE_FORMAT_DATETIME);
        Wrapper<OrderExpirationDateDTO> wrapper = new EntityWrapper<>();
        wrapper.le("expiration_time", currentTime);
        wrapper.in("status", Arrays.asList("1", "2"));//1待签合同、2待付款
        wrapper.orderBy("order_id", true);
        wrapper.gt("order_id", lastNo);
        Page<OrderExpirationDateDTO> page = new Page<>(0, 50);
        return baseMapper.selectPage(page, wrapper);
    }

    @Override
    public boolean add(OrderExpirationDateSaveBO saveBO) {
        OrderExpirationDateDTO dto = new OrderExpirationDateDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public OrderExpirationDateDTO getByOrderId(Long orderId) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("order_id", orderId);
        return selectOne(wrapper);
    }

    /**
     * 更新过期时间 根据订单id
     *
     * @param orderExpirationDateDTO
     * @return
     */
    public boolean updateByOrderId(OrderExpirationDateDTO orderExpirationDateDTO) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("order_id", orderExpirationDateDTO.getOrderId());
        return update(orderExpirationDateDTO, wrapper);
    }
}
